package com.jackwoo.jerrymouse.util;

import com.jackwoo.jerrymouse.logger.FileUtilLogger;
import org.slf4j.Logger;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

/**
 * Author: StupidZhe
 * Date: 2019/11/19
 * Desc:
 **/
public class FileUtil {

    private static Logger log = FileUtilLogger.getInstance();


    public static Map<String, String> jspScan(String rootPath) throws IOException {
        File root = new File(rootPath);
        if (!root.isDirectory()) {
            throw new FileNotFoundException("file path is not dir");
        }

        Map<String, String> jspMap = new HashMap<>(10);
        File[] jspz = root.listFiles();
        for (File jsp : jspz) {
            String[] as = jsp.getName().split("[.]");
            if ("CLASS".equalsIgnoreCase(as[as.length - 1])) {
                FileInputStream inputStream = new FileInputStream(jsp);
                try {
                    boolean res = ClassUtil.classCheck(inputStream.readAllBytes(), "HttpServlet");
                    if (res) {
                        jspMap.put(jsp.getName(), jsp.getAbsolutePath());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new FileNotFoundException("JSP read fail");
                }
            }
        }
        return jspMap;
    }

    public static File fileFind(String absolutePath) throws FileNotFoundException {
        File file = new File(absolutePath);
        if (!file.exists()) {
            throw new FileNotFoundException("file not exists");
        }
        return file;
    }

    public static String getContent(File file) throws IOException {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
        StringBuilder content = new StringBuilder();
        byte[] tmp = new byte[reader.available()];

        int n;
        while ((n = reader.read(tmp)) != -1) {
            for (int k = 0; k < n; k++) {
                content.append((char) tmp[k]);
            }
        }
        return content.toString();
    }

    public static byte[] getContent1(File file) throws IOException {
        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
        byte[] tmp = new byte[reader.available()];

        while ((reader.read(tmp)) != -1) {

        }

        return tmp;
    }

    //    public static void main(String[] args) throws IOException {
//        BufferedInputStream reader = new BufferedInputStream(new FileInputStream(new File("/Users/StupidZhe/IT/JerryMouse/web-root/test/logo.png")));
//        StringBuilder content = new StringBuilder();
//        byte[] tmp = new byte[reader.available()];
//        int n;
//        while ((n = reader.read(tmp)) != -1) {
//            for (int k = 0; k < n; k++) {
//                content.append((char)tmp[k]);
//            }
//        }
//        File file = new File("/Users/StupidZhe/IT/JerryMouse/web-root/test/logo1.png");
//        if (!file.exists()) {
//            file.createNewFile();
//        }
//        BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(file));
//        writer.write(tmp);
//
//        for (byte b : tmp) {
//            content.append((char)b);
//        }
//        System.out.println(content);
//    }
    public static void main(String[] args) throws IOException {
        URL url = new URL("https://res.cloudinary.com/jackwoo/image/upload/v1575006452/jack%20woo/logo_gco7no.png");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        // 设置连接方式：get
        connection.setRequestMethod("GET");
        connection.connect();
        StringBuilder stringBuilder = new StringBuilder();
        BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File("/Users/StupidZhe/IT/JerryMouse/web-root/test/test1")));

        if (connection.getResponseCode() == 200) {
            BufferedInputStream is = new BufferedInputStream(connection.getInputStream());
            // 存放数据
            byte[] tmp = new byte[is.available()];
            int n;
            while ((n = is.read(tmp)) != -1) {
                stream.write(tmp, 0, n);
                for (int k = 0; k < n; k++) {
                    stringBuilder.append((char)tmp[k]);
                }
            }
            stream.flush();
            stream.close();
            System.out.println(stringBuilder.toString());
        }
    }
}
